
package com.android.formula.activity;

import java.util.ArrayList;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;

import com.android.formula.R;
import com.android.formula.adapter.TBaseAdapter;
import com.android.formula.adapter.TypeFormulaListAdapter;
import com.android.formula.model.NoteSubject;
import com.android.formula.model.TypeFormula;
import com.android.formula.task.DeleteTypeFormulaDBTask;
import com.android.formula.task.GetTypeFormulaDBTask;
import com.android.formula.task.IDataEventHandler;
import com.android.formula.utils.Constants;

public class TypeFormulaNoteActivity extends BaseGridViewActivity<TypeFormula> implements
        OnItemLongClickListener {

    private PopupMenu mPopupMenu;

    @Override
    protected int getLayout() {
        return R.layout.activity_gridview;
    }

    @Override
    public void initView() {
        super.initView();
        getActionBar().setDisplayHomeAsUpEnabled(true);
        
        mGridView.setOnItemLongClickListener(this);
    }

    @Override
    protected void getData() {
        super.getData();
        getDialogsManager().showProgress(R.string.loading_msg);
        Bundle bun = getIntent().getExtras();
        NoteSubject params = (NoteSubject) bun.getSerializable(Constants.DATA_KEY);
        this.setTitle(params.getSubjectName());
        new GetTypeFormulaDBTask(this, params, new IDataEventHandler<ArrayList<TypeFormula>>() {

            @Override
            public void onNotifyNoData() {
                getDialogsManager().dismissProgress();
                showNoData();
            }

            @Override
            public void onNotifyErrorMessage(String title, String message) {
                getDialogsManager().dismissProgress();
                showNoData();
                getDialogsManager().showErrorDialog(R.string.error_title, R.string.error_msg, 0,
                        null);
            }

            @Override
            public void onNotifyData(ArrayList<TypeFormula> data) {
                getDialogsManager().dismissProgress();
                mListItemDatas.addAll(data);
                if (mListItemDatas != null && mListItemDatas.size() > 0) {
                    showData();
                } else {
                    showNoData();
                }
            }
        }).execute(params);
    }

    @Override
    protected void clickOnPos(TypeFormula clickedItem, int pos) {
        Intent i = new Intent(this, NoteDetailActivity.class);
        Bundle bun = new Bundle();
        bun.putSerializable(Constants.DATA_KEY, clickedItem);
        i.putExtras(bun);
        startActivity(i);
    }

    @Override
    protected TBaseAdapter<TypeFormula> getAdapter() {
        return new TypeFormulaListAdapter(this, mListItemDatas);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        final TypeFormula typeFormula = (TypeFormula) parent.getItemAtPosition(position);
        mPopupMenu = new PopupMenu(TypeFormulaNoteActivity.this, view);
        mPopupMenu.getMenu().add(Menu.NONE, Constants.UPDATE_ITEM_MENU_ID, Menu.NONE,
                R.string.btn_update_text);
        mPopupMenu.getMenu().add(Menu.NONE, Constants.DELETE_ITEM_MENU_ID, Menu.NONE,
                R.string.btn_delete_text);
        mPopupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case Constants.UPDATE_ITEM_MENU_ID:
                        updateFormula(typeFormula);
                        break;
                    case Constants.DELETE_ITEM_MENU_ID:
                        getDialogsManager().showConfirmDialog(R.string.delete_title,
                                R.string.delete_subject_msg, R.string.btn_yes,
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        deleteFormula(typeFormula);
                                        mListItemDatas.remove(typeFormula);
                                        mAdapter.notifyDataSetChanged();
                                        dialog.dismiss();
                                    }
                                }, R.string.btn_no, null, true);
                        break;
                    default:
                        break;
                }
                return false;
            }
        });

        mPopupMenu.show();
        return true;
    }

    private void updateFormula(TypeFormula typeFormula) {
        Intent i = new Intent(this, EditNoteActivity.class);
        Bundle bun = new Bundle();
        bun.putSerializable(Constants.DATA_KEY, typeFormula);
        i.putExtras(bun);
        startActivity(i);
    }

    private void deleteFormula(TypeFormula typeFormula) {
        new DeleteTypeFormulaDBTask(this, typeFormula, null, null).execute(typeFormula);
    }

}
